<template>
  <div>
    <a-modal
      :title="formData.id ? '编辑代理' : '添加代理'"
      :width="800"
      :confirm-loading="modalLoading"
      :mask-closable="false"
      :destroy-on-close="true"
      v-model="visible"
      @cancel="handleCancel"
      @ok="handleOk"
    >
      <a-form-model ref="formData" :model="formData" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
        <a-form-model-item
          label="选择用户："
          prop="user_id"
          :rules="[{ required: true, message: '请选择用户', trigger: ['blur', 'change'] }]"
        >
          <div class="flex">
            <a-input
              class="inp"
              style="width: 200px; margin-right: 10px"
              disabled
              v-model="userInfo.nickname"
              placeholder="请选择用户"
            ></a-input>
            <a-button type="primary" @click="$refs['selectUser'].visible = true"> 选择 </a-button>
          </div>
        </a-form-model-item>
        <a-form-model-item label="股东比例：" prop="holder_rate">
          <a-input-number :min="0" v-model="formData.holder_rate" /> %
        </a-form-model-item>
        <a-form-model-item
          label="真实姓名："
          prop="name"
          :rules="[{ required: true, message: '请输入真实姓名', trigger: ['blur', 'change'] }]"
        >
          <a-input v-model="formData.name" placeholder="请输入真实姓名"></a-input>
        </a-form-model-item>
        <a-form-model-item
          label="身份证号："
          prop="idcard"
          :rules="[
            { required: true, message: '请输入身份证号', trigger: ['blur', 'change'] },
            {
              pattern: /^[1-9]\d{5}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/,
              message: '身份证号码格式不正确!',
            },
          ]"
        >
          <a-input v-model="formData.idcard" placeholder="请输入身份证号"></a-input>
        </a-form-model-item>
        <a-form-model-item label="状态：" prop="status">
          <a-radio-group v-model="formData.status">
            <a-radio :value="1">通过</a-radio>
            <a-radio :value="2">拒绝</a-radio>
            <!-- <a-radio :value="3">不达标</a-radio> -->
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="考核是否达标：" prop="check">
          <a-radio-group v-model="formData.check">
            <a-radio :value="1">是</a-radio>
            <a-radio :value="0">否</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item
          label="拒绝原因："
          prop="refuse_reason"
          v-if="formData.status == 2"
          :rules="[{ required: true, message: '请输入拒绝原因', trigger: ['blur', 'change'] }]"
        >
          <a-textarea
            v-model="formData.refuse_reason"
            placeholder="请输入拒绝原因"
            :auto-size="{ minRows: 3, maxRows: 5 }"
          />
        </a-form-model-item>
        <a-form-model-item label="身份：" prop="role">
          <a-radio-group v-model="formData.role">
            <a-radio :value="1">区域代表</a-radio>
            <a-radio :value="2">区域股东</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="级别：" prop="area_level">
          <a-radio-group v-model="formData.area_level" @change="levelChange">
            <a-radio :value="1">省代</a-radio>
            <a-radio :value="2">市代</a-radio>
            <a-radio :value="3">区代</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <!-- <a-form-model-item label="是否发放奖金：" prop="add_profit">
          <a-radio-group v-model="formData.add_profit">
            <a-radio :value="1">是</a-radio>
            <a-radio :value="0">否</a-radio>
          </a-radio-group>
        </a-form-model-item> -->
        <a-form-model-item
          label="区域选择："
          prop="province_id"
          :rules="[{ required: true, message: '请选择区域', trigger: ['blur', 'change'] }]"
        >
          <Address v-model="addressInfo" :showNum="formData.area_level" @input="addressChange"></Address>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
    <!-- 选择用户 -->
    <selectUser ref="selectUser" type="areaAgent" @select="select"></selectUser>
  </div>
</template>

<script>
import SelectUser from '@/components/SelectUser/newSelectUser'
import { Address } from '@/components'
import { agentSaveApi, agentInfoApi } from '@/api/area'
import { check } from '@/api/setting/applet'
export default {
  name: 'editForm',
  components: { SelectUser, Address },
  props: {},
  data() {
    return {
      visible: false,
      modalLoading: false,
      formData: {
        id: '',
        name: '',
        idcard: '',
        area_level: 1,
        role: 1,
        province_id: '',
        province: '',
        city_id: '',
        city: '',
        district_id: '',
        district: '',
        user_id: '',
        holder_rate: '',
        status: 1,
        refuse_reason: '',
        add_profit: 0,
        check: 1,
      },
      userInfo: {},
      addressInfo: {},
      address_data: {},
    }
  },
  created() {},
  methods: {
    getInfo() {
      var _self = this
      agentInfoApi(_self.formData.id)
        .then((res) => {
          if (res.code == 0) {
            var data = res.data
            _self.formData = data
            if (data.user) _self.$set(_self.userInfo, 'nickname', data.user.nickname)
            _self.addressInfo = { province_id: data.province_id, city_id: data.city_id, district_id: data.district_id }
            _self.visible = true
          } else {
            _self.$message.error(res.msg)
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    handleCancel() {
      this.formData = {
        id: '',
        name: '',
        idcard: '',
        area_level: 1,
        role: 1,
        province_id: '',
        province: '',
        city_id: '',
        city: '',
        district_id: '',
        district: '',
        user_id: '',
        holder_rate: '',
        status: 1,
        refuse_reason: '',
        add_profit: 0,
      }
      this.$refs.formData.resetFields()
    },
    handleOk() {
      var _self = this
      _self.$refs.formData.validate((valid) => {
        if (valid) {
          _self.modalLoading = true
          agentSaveApi(_self.formData)
            .then((res) => {
              _self.modalLoading = false
              if (res.code === 0) {
                _self.$message.success(res.msg)
                _self.visible = false
                _self.$emit('confirm')
              } else {
                _self.$message.error(res.msg)
              }
            })
            .catch((err) => {
              _self.modalLoading = false
              console.log(err, 'err')
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    select(e) {
      this.userInfo = e
      this.formData.user_id = e.id
    },
    addressChange(e) {
      this.address_data = e
      if (e.province_id) {
        this.formData.province_id = e.province_id
        this.formData.province = e.province
      }
      if (e.city_id) {
        this.formData.city_id = e.city_id
        this.formData.city = e.city
      }
      if (e.district_id) {
        this.formData.district_id = e.district_id
        this.formData.district = e.district
      }
    },
    levelChange() {
      if (this.address_data.province_id) {
        this.formData.city_id = ''
        this.formData.city = ''
        this.formData.district_id = ''
        this.formData.district = ''
        if (this.formData.area_level >= 2) {
          this.formData.city_id = this.address_data.city_id
          this.formData.city = this.address_data.city
        }
        if (this.formData.area_level >= 3) {
          this.formData.district_id = this.address_data.district_id
          this.formData.district = this.address_data.district
        }
      }
      console.log(this.formData, 222)
    },
  },
}
</script>

<style lang="scss"></style>
